package com.exam.sacrifice.widget;

import android.os.Handler;
import android.widget.TextView;

public class FadeInTypewriter {
    private TextView textView;
    private long charDelay = 100;
    private long fadeDuration = 300;

    public FadeInTypewriter(TextView textView) {
        this.textView = textView;
    }

    public void animateText(String text) {
        textView.setText("");
        textView.setAlpha(0f);

        for (int i = 0; i <= text.length(); i++) {
            final int finalI = i;
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    textView.setText(text.substring(0, finalI));
                    if (finalI > 0) {
                        textView.animate()
                                .alpha(1f)
                                .setDuration(fadeDuration)
                                .start();
                    }
                }
            }, i * charDelay);
        }
    }

    public void setCharDelay(long delay) {
        this.charDelay = delay;
    }

    public void setFadeDuration(long duration) {
        this.fadeDuration = duration;
    }
}
